/**
 * @namespace
 * @desc SC 은행 Common 라이브러리의 Data Service 네임스페이스
 */

ScLibs.DataService = {
    _baseUrl: 'http://localhost:8000',
    defaultData: {
        header: 'header',
        customer: {
            name: 'John',
            email: 'john@bluedigm.com',
            accountNumber: '123-456'
        }
    }
};

(function(){

// IF-001
amplify.request.define( "login", "ajax", {
    url: ScLibs.DataService._baseUrl + "/login.json",
    dataType: "json",
    type: "GET"
});

// IF-002
amplify.request.define( "list", "ajax", {
    url: ScLibs.DataService._baseUrl + "/list.json",
    dataType: "json",
    type: "GET"
});


/**
 * @function
 * @memberof ScLibs.DataService
 * @param {String} resource
 * @param {Object} data
 * @desc Call a server API (via AJAX).
 * @example
 * ScLibs.DataService.request('login', {
 *     id: 'john',
 *     pass: 'secret'
 * }).then( function (data) {
 *     console.log('login successful');
 * }, function (error) {
 *     console.log('login failed');
 * }});
 * @example
 * var deferredList = ScLibs.DataService.request('list');
 *
 * deferredList.then(function (data) {
 *     console.log('login successful');
 * }, function (error) {
 *     console.log('login failed');
 * }});
 */

ScLibs.DataService.request = function (resource, data) {
    // from http://www.elijahmanor.com/adding-jquery-deferred-support-to-amplifyjs-request/

    var payload = $.extend({}, ScLibs.DataService.defaultData);

    if (typeof data !== "undefined") {
        payload.data = data;
    }

    return payload; // TEST

    return $.Deferred(function( dfd ) {
        amplify.request({
            resourceId: resource,
            data: payload,
            success: dfd.resolve,
            error: dfd.reject
        });
    }).promise();
};

}());

